﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace mVu
{
  class YouJizz : Site
  {
    public override Dictionary<string, string> GetCategories()
    {
      var categories = new Dictionary<string, string>
                         {
                           {String.Empty, "http://www.youjizz.com"}
                         };
      return categories;
    }

    public override Dictionary<string, string> GetSorts()
    {
      var sorts = new Dictionary<string, string>
                    {
                      {String.Empty, String.Empty}
                    };

      return sorts;
    }

    public override Dictionary<string, string> GetMediaTypes()
    {
      var types = new Dictionary<string, string>
                    {
                      {"FLV", "FLV"}
                    };
      return types;
    }

    public override List<Thumbnail> GetThumbnails(string baseAddress, string sort, string query, int page)
    {
      var list = new List<Thumbnail>();

      try
      {
        if (String.IsNullOrEmpty(baseAddress))
        {
          baseAddress = "http://www.youjizz.com";
        }

        string address = String.IsNullOrEmpty(query) ? String.Format("{0}/page/{1}.html", baseAddress, page) : String.Format("{0}/search/{1}.html", baseAddress, query.Replace(" ", "-"), page);
        string result = GetPageSource(address);

        if (!String.IsNullOrEmpty(result))
        {
          var rh = new SgmlReaderDll.SGMLReaderHelper();
          string xhtml = rh.ProcessString(result);

          XDocument doc = XDocument.Parse(xhtml);
          XNamespace ns = XNamespace.Get("http://www.w3.org/1999/xhtml");

          var nodes = doc.Descendants(ns + "span").Where(n => n.Attribute("id") != null && n.Attribute("id").Value.Equals("miniatura"));

          list.AddRange(nodes.Select(node => new Thumbnail
                                               {
                                                 Image = node.Descendants(ns + "img").First().Attribute("src").Value, 
                                                 Url = String.Format("http://www.youjizz.com{0}", node.Descendants(ns + "a").First().Attribute("href").Value), 
                                                 Title = node.Descendants(ns + "span").Where(n => n.Attribute("id") != null && n.Attribute("id").Value == "title1").First().Value, 
                                                 Length = String.Format("({0})", node.Descendants(ns + "span").Where(n => n.Attribute("class") != null && n.Attribute("class").Value == "thumbtime").First().Descendants(ns + "span").First().Value)
                                               }));
        }
      }
      catch (Exception ex)
      {
        Utilities.HandleError(ex);
      }

      return list;
    }

    public override Dictionary<string, string> GetVideoPaths(string url)
    {
      var dictionary = new Dictionary<string, string>();

      try
      {
        if (!String.IsNullOrEmpty(url))
        {
          string result = GetPageSource(url);

          if (!String.IsNullOrEmpty(result))
          {
            int start = result.IndexOf("http://media");
            int end = result.IndexOf(".flv", start + 1) + 4;
            dictionary.Add("FLV", result.Substring(start, end - start));
          }
        }
      }
      catch (Exception ex)
      {
        Utilities.HandleError(ex);
      }

      return dictionary;
    }
  }
}
